﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace School.SystemManage.WebPages.SchoolIntro
{
    public partial class TeacherEdit : System.Web.UI.Page
    {
        TechnologySchoolEntities db = new TechnologySchoolEntities();
        Teachers teacher = null;
        bool isAdd = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            isAdd = Request.QueryString["action"] == "add";
            if (isAdd)
            {
                teacher = new Teachers();
            }
            else
            {
                int tid = int.Parse(Request.QueryString["tid"]);
                teacher = db.Teachers.FirstOrDefault(t => t.TID == tid);
                if (teacher != null)
                {
                    if (!Page.IsPostBack)
                    {
                        BindData(teacher);
                    }
                }
            }
        }

        private void BindData(Teachers teacher)
        {
            ddl_Position.SelectedIndex = int.Parse(teacher.Position.ToString());
            txbPositionName.Text = teacher.PositionName;
            ddl_Major.SelectedIndex = int.Parse(teacher.Major.ToString());
            txbTeacherName.Text = teacher.TeacherName;
            imgPhone.ImageUrl = teacher.PhotoPath;
            editor.Value = teacher.Info;
        }

        protected void tbnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string imgPath = string.Empty;
                if (FileUpload1.HasFile)
                {
                    if (!UploadPhoto(out imgPath))
                    {
                        lblMsg.Text = "上传照片失败！";
                        return;
                    }
                }
                else
                {
                    if (!isAdd)
                    {
                        imgPath = teacher.PhotoPath;
                    }
                }

                teacher.Position = byte.Parse(ddl_Position.SelectedValue.ToString());
                teacher.PositionName = txbPositionName.Text;
                teacher.Major = byte.Parse(ddl_Major.SelectedValue.ToString());
                teacher.TeacherName = txbTeacherName.Text;
                teacher.PhotoPath = imgPath;
                teacher.Info = editor.Value;

                if (isAdd)
                {
                    db.AddToTeachers(teacher);
                }

                if (db.SaveChanges() > 0)
                {
                    lblMsg.Text = "编辑成功！";
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "发生异常，原因：" + ex.Message;
            }
        }

        private bool UploadPhoto(out string imagePath)
        {
            imagePath = string.Empty;
            try
            {
                string originalFileName = FileUpload1.FileName;
                string extendName = originalFileName.Substring(originalFileName.LastIndexOf('.') + 1);
                string newFileName = Guid.NewGuid().ToString() + "." + extendName;
                string serverSavePath = Server.MapPath(@"~\UploadFiles\TeacherPhone");
                imagePath = System.IO.Path.Combine(@"\UploadFiles\TeacherPhone", newFileName);
                FileUpload1.SaveAs(System.IO.Path.Combine(serverSavePath, newFileName));

                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


    }
}
